package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.solidWaste;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CCompanys;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.TFactoryHandlingPersonnel;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.TFactoryProcessInfo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl.CCompanysServiceImpl;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl.CPdtClassServiceImpl;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl.TFactoryHandlingPersonnelServiceImpl;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl.TFactoryProcessInfoServiceImpl;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.FactoryHandlingPersonnelVo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.FactoryProcessInfoVo;
import com.xiaoxu.intranetweb.util.StringUtils;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@CrossOrigin()
@RestController
@RequestMapping(value = "/solidWaste")
public class SolidWasteController {
    @Autowired
    private TFactoryHandlingPersonnelServiceImpl factoryHandlingPersonnelService;
    @Autowired
    private TFactoryProcessInfoServiceImpl factoryProcessInfoService;
    @Autowired
    private CPdtClassServiceImpl pdtClassService;
    @Autowired
    private CCompanysServiceImpl companysService;
    @GetMapping("/getFactoryProcessInfo")
    public SzResult getfactoryProcessInfo(@RequestParam(value = "cpyId", required = false) String cpyId) {
        QueryWrapper<TFactoryProcessInfo> factoryProcessInfoQueryWrapper = new QueryWrapper<>();
        factoryProcessInfoQueryWrapper.eq(StringUtils.isNotEmpty(cpyId),"CPY_ID",cpyId);
        List<FactoryProcessInfoVo> list = factoryProcessInfoService.list(factoryProcessInfoQueryWrapper).stream().map(item -> {
            FactoryProcessInfoVo factoryProcessInfoVo = new FactoryProcessInfoVo();
            BeanUtils.copyProperties(item, factoryProcessInfoVo);
            CCompanys companys = companysService.getById(item.getCpyId());
            if (companys != null) {
                factoryProcessInfoVo.setCo(companys.getCpyCo() + "." + companys.getCpyChico());
            }
            return factoryProcessInfoVo;
        }).collect(Collectors.toList());
        return SzResult.success(list);
    }
    @PostMapping("/addFactoryProcessInfo")
    public SzResult addFactoryProcessInfo(@RequestBody TFactoryProcessInfo factoryProcessInfo) {
        return factoryProcessInfoService.addFactoryProcessInfo(factoryProcessInfo);
    }
    @PostMapping("/updateFactoryProcessInfo")
    public SzResult updateFactoryProcessInfo(@RequestBody TFactoryProcessInfo factoryProcessInfo) {
        return factoryProcessInfoService.updateFactoryProcessInfo(factoryProcessInfo);
    }
    @PostMapping("/delFactoryProcessInfo")
    public SzResult delFactoryProcessInfo(@RequestBody TFactoryProcessInfo factoryProcessInfo){
        return SzResult.success(factoryProcessInfoService.removeById(factoryProcessInfo.getId()));
    }
    @GetMapping("/getFactoryHandlingPersonnel")
    public SzResult getFactoryHandlingPersonnel(@RequestParam(value = "pdtName", required = false) String pdtName,
                                                @RequestParam(value = "pdtClassId", required = false) String pdtClassId) {
        QueryWrapper<TFactoryHandlingPersonnel> factoryHandlingPersonnelQueryWrapper = new QueryWrapper<>();
        factoryHandlingPersonnelQueryWrapper.eq(StringUtils.isNotEmpty(pdtName),"PDT_NAME",pdtName)
                .eq(StringUtils.isNotEmpty(pdtClassId),"PDT_NAME",pdtClassId)
                .orderByDesc("PDT_CLASS_ID");
        List<FactoryHandlingPersonnelVo> list = factoryHandlingPersonnelService.list(factoryHandlingPersonnelQueryWrapper).stream().map(item -> {
            FactoryHandlingPersonnelVo factoryHandlingPersonnelVo = new FactoryHandlingPersonnelVo();
            BeanUtils.copyProperties(item, factoryHandlingPersonnelVo);
            factoryHandlingPersonnelVo.setPdtClass(pdtClassService.getById(item.getPdtClassId()).getPdtclassClass());
            return factoryHandlingPersonnelVo;
        }).collect(Collectors.toList());
        return SzResult.success(list);
    }
    @PostMapping("/addFactoryHandlingPersonnel")
    public SzResult addFactoryHandlingPersonnel(@RequestBody TFactoryHandlingPersonnel factoryHandlingPersonnel) {
        return factoryHandlingPersonnelService.addFactoryProcessInfo(factoryHandlingPersonnel);
    }
    @PostMapping("/updateFactoryHandlingPersonnel")
    public SzResult updateFactoryHandlingPersonnel(@RequestBody TFactoryHandlingPersonnel factoryHandlingPersonnel){
        return factoryHandlingPersonnelService.updateFactoryProcessInfo(factoryHandlingPersonnel);
    }
    @PostMapping("/delFactoryHandlingPersonnel")
    public SzResult delFactoryHandlingPersonnel(@RequestBody TFactoryHandlingPersonnel factoryHandlingPersonnel) {
        return SzResult.success(factoryHandlingPersonnelService.removeById(factoryHandlingPersonnel.getId()));
    }
}
